package com.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.javabean.Permission;
import com.javabean.Role;
import com.javabean.RolePermission;
import com.service.PermissionService;
import com.service.RoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Controller
@RequestMapping("/role")
public class RoleController {
    @Autowired
    RoleService roleService;
    @Autowired
    PermissionService permissionService;
    @RequestMapping("/findAll")
    public String findAll(HttpServletRequest request, @RequestParam(value = "page",defaultValue = "1") int page, @RequestParam(value = "limit",defaultValue = "5")  int limit){
        PageHelper.startPage(page,limit);
        List<Role> all = roleService.findAll();
        PageInfo<Role> pageInfo=new PageInfo<>(all);
        request.setAttribute("pageInfo",pageInfo);
        return "/role/role-list";
    }

    @RequestMapping(value = "/add",method = RequestMethod.GET)
    public String add(){
        return "/role/role-add";
    }

    @RequestMapping(value = "/add",method = RequestMethod.POST)
    public String add(Role role){
        roleService.add(role);
        return "redirect:findAll";
    }

    @RequestMapping("/delete")
    public String delete(int id){
        roleService.delete(id);
        return "redirect:findAll";
    }

    @RequestMapping("/show")
    public String show(HttpServletRequest request,int id){
        Role role = roleService.show(id);
        request.setAttribute("role",role);
        return "/role/role-show";
    }

    @RequestMapping(value = "/edit",method = RequestMethod.GET)
    public String edit(HttpServletRequest request,int id){
        Role role = roleService.show(id);
        request.setAttribute("role",role);
        return "/role/role-edit";
    }

    @RequestMapping(value = "/edit",method = RequestMethod.POST)
    public String edit(Role role){
        roleService.edit(role);
        return "redirect:findAll";
    }

    @RequestMapping(value = "/permissionAdd",method = RequestMethod.GET)
    public String permissionAdd(HttpServletRequest request,int id){
        List<Permission> all = permissionService.findAll();
        List<Permission> byRid = permissionService.findByRid(id);
        request.setAttribute("roleId", id);
        request.setAttribute("permissionList", all);
        request.setAttribute("permissionIdList", byRid);
        return "/role/role-edit-permission";
    }
    @RequestMapping(value = "/permissionAdd",method = RequestMethod.POST)
    public String permissionAdd(@RequestBody RolePermission rolePermission){
        roleService.permissionAdd(rolePermission);
        return "redirect:findAll";
    }
}
